﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Transactions;
using DataAccess;
using Common;

#region VƯƠNG TRÍ LỢI
namespace BusinessLogic
{
    public class ChiTietPhieuTTNCC : MarshalByRefObject
    {
        private static SqlCommand cmd;
        private static ArrayList arr;
        private static SqlDataReader dr;
        private static SqlConnection cn;


        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.ChiTietPhieuTTNCC[] Select(string maphieuTTNCC)
        {
            try
            {
                string sql = Common.Constants.ChiTietPhieuTTNCC.SelectChiTietPhieuTTNCC;

                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTNCC.MaPhieuTTNCC, SqlDbType.VarChar, 50).Value = maphieuTTNCC;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietPhieuTTNCC row = new Entities.ChiTietPhieuTTNCC();
                    row.MaHoaDonNhap = (dr[Common.Constants.ChiTietPhieuTTNCC.MaHoaDon].ToString());
                    row.MaPhieuTTNCC = dr[Common.Constants.ChiTietPhieuTTNCC.MaPhieuTTNCC].ToString();
                    row.TongTienThanhToan = (dr[Common.Constants.ChiTietPhieuTTNCC.TongTien].ToString());
                    row.SoNo = (dr[Common.Constants.ChiTietPhieuTTNCC.TienNo].ToString());
                    row.ThanhToan = (dr[Common.Constants.ChiTietPhieuTTNCC.ThanhToan].ToString());
                    row.GhiChu = dr[Common.Constants.ChiTietPhieuTTNCC.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.ChiTietPhieuTTNCC.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return null;

                Entities.ChiTietPhieuTTNCC[] arrC = new Entities.ChiTietPhieuTTNCC[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.ChiTietPhieuTTNCC)arr[i];
                }

                //Giai phong bo nho
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return arrC;
            }
            catch
            { return new Entities.ChiTietPhieuTTNCC[0]; }

        }

        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.ChiTietPhieuTTNCC[] Select()
        {
            try
            {
                string sql = Common.Constants.ChiTietPhieuTTNCC.SelectChiTietPhieuTTNCC1;

                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietPhieuTTNCC row = new Entities.ChiTietPhieuTTNCC();
                    row.MaHoaDonNhap = (dr[Common.Constants.ChiTietPhieuTTNCC.MaHoaDon].ToString());
                    row.MaPhieuTTNCC = dr[Common.Constants.ChiTietPhieuTTNCC.MaPhieuTTNCC].ToString();
                    row.TongTienThanhToan = (dr[Common.Constants.ChiTietPhieuTTNCC.TongTien].ToString());
                    row.SoNo = (dr[Common.Constants.ChiTietPhieuTTNCC.TienNo].ToString());
                    row.ThanhToan = (dr[Common.Constants.ChiTietPhieuTTNCC.ThanhToan].ToString());
                    row.GhiChu = dr[Common.Constants.ChiTietPhieuTTNCC.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.ChiTietPhieuTTNCC.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return null;

                Entities.ChiTietPhieuTTNCC[] arrC = new Entities.ChiTietPhieuTTNCC[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.ChiTietPhieuTTNCC)arr[i];
                }

                //Giai phong bo nho
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return arrC;
            }
            catch { return new Entities.ChiTietPhieuTTNCC[0]; }
        }

        public static bool InsertUpdate(Entities.ChiTietPhieuTTNCC chitietphieuttncc)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.ChiTietPhieuTTNCC.InsertUpdate;
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTNCC.HanhDong, SqlDbType.NVarChar, 50).Value = chitietphieuttncc.HanhDong;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTNCC.MaHoaDon, SqlDbType.VarChar, 50).Value = chitietphieuttncc.MaHoaDonNhap;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTNCC.MaPhieuTTNCC, SqlDbType.VarChar, 50).Value = chitietphieuttncc.MaPhieuTTNCC;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTNCC.TongTien, SqlDbType.Float).Value = chitietphieuttncc.TongTienThanhToan;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTNCC.TienNo, SqlDbType.Float).Value = chitietphieuttncc.SoNo;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTNCC.ThanhToan, SqlDbType.Float).Value = chitietphieuttncc.ThanhToan;
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTNCC.GhiChu, SqlDbType.NVarChar, 200).Value = "";
                    cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTNCC.Deleted, SqlDbType.Bit).Value = false;

                    int i = cmd.ExecuteNonQuery();
                    bool kt = false;
                    if (i == 1)
                        kt = true;
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;

                    ts.Complete();
                    return kt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message;
                return false;
            }

        }

        public static bool InsertUpdate(Entities.ChiTietPhieuTTNCC[] chitietphieuttncc)
        {
            try
            {
                for (int i = 0; i < chitietphieuttncc.Length; i++)
                {
                    InsertUpdate(chitietphieuttncc[i]);
                }
                return true;
            }
            catch 
            {
                return false;
                
            }
    
        }

    }
}
#endregion